package com.itmuch.cloud.controller;

import com.itmuch.cloud.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/user/{id}")
    public User findById1(@PathVariable Long id) {
        return restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
    }

    @GetMapping("/log-instance1")
    public void logInstance11() {
        // 获取服务实例列表
        ServiceInstance instances = loadBalancerClient.choose("microservice-provider-user");
        log.info("serviceId: {} ,host: {}, port: {}", instances.getServiceId(), instances.getHost(), instances.getPort());
    }

    @GetMapping("/log-instance2")
    public void logInstance12() {
        // 获取服务实例列表
        ServiceInstance instances = loadBalancerClient.choose("microservice-provider-user-metadata");
        log.info("serviceId: {} ,host: {}, port: {}", instances.getServiceId(), instances.getHost(), instances.getPort());
    }
}
